package com.nuanshui.heatedloan.controller.display.module;

import com.alibaba.dubbo.config.annotation.Reference;
import com.nuanshui.framework.behavior.BehaviorContext;
import com.nuanshui.framework.lang.SysStringUtils;
import com.nuanshui.heatedloan.constant.GlobalParam;
import com.nuanshui.heatedloan.controller.BaseController;
import com.nuanshui.heatedloan.credit.constant.PersonPageStatusConstant;
import com.nuanshui.heatedloan.credit.facade.ExecuteCustomAuthService;
import com.nuanshui.heatedloan.credit.response.PersonPageStatusResponse;
import com.nuanshui.heatedloan.middleware.rabbitmq.RabbitmqMessageSender;
import com.nuanshui.heatedloan.module.facade.IModuleBuilder;
import com.nuanshui.heatedloan.module.facade.ModuleBuilderFactory;
import com.nuanshui.heatedloan.module.facade.ModuleService;
import com.nuanshui.heatedloan.module.model.Feature;
import com.nuanshui.heatedloan.module.model.ModuleForm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/modules")
public class ModuleController extends BaseController{
	
	@Autowired
	private ModuleService moduleService;

	@Reference(url= GlobalParam.CREDIT_DUBBO_SERVER,timeout= GlobalParam.DUBBO_SERVER_TIME_OUT,version = GlobalParam.CREDIT_DUBBO_SERVER_VERSION)
	private ExecuteCustomAuthService executeCustomAuthService;

	private static final Logger logger  = LoggerFactory.getLogger(ModuleController.class);

	@Autowired
	private RabbitmqMessageSender rabbitmqMessageSender;


	@RequestMapping(value = "/getMuseumFullModule",method = RequestMethod.POST)
	public String getMuseumFullModule(ModuleForm form,Pageable pager){
		String userId = getCurrentUserNotCheckEmptyId().getId();

		BehaviorContext.getContext().put("userId", userId);

		if(SysStringUtils.isNotEmpty(form.getExhibitRoomId()) && SysStringUtils.isNullOrEmpty(form.getMuseumId()))
		{
		}

		IModuleBuilder builder = ModuleBuilderFactory.getModuleBuilderFactory(form);
		Object result = null;
		if(builder != null){
			form.setPager(pager);
			result = builder.getResult(form);
		}
		return success(result);
	}


	@RequestMapping(value = "/personal",method = RequestMethod.POST)
	public String getPersonModule() {
		PersonPageStatusResponse personPageStatusResponse =new PersonPageStatusResponse();
		if(SysStringUtils.isEmpty(getCurrentUserNotCheckEmptyId().getId())){
			personPageStatusResponse.setStatus(PersonPageStatusConstant.NOLOGIN);
		}else{
			personPageStatusResponse=executeCustomAuthService.getPersonPageStatus(getCurrentUserNotCheckEmptyId().getId());
		}
		Map<String,Object> data=new HashMap<String,Object>();
		BehaviorContext.getContext().put("userId", getCurrentUserNotCheckEmptyId().getId());
		BehaviorContext.getContext().put("personPageStatusResponse",personPageStatusResponse);
		List<Feature> module = moduleService.getPersonModule();
		data.put("list",module);
		data.put("personState",personPageStatusResponse);
		return success(data);
	}
}
